package com.scuec.demo1.controller;

import com.github.pagehelper.PageInfo;
import com.scuec.demo1.entity.StudentInfo;
import com.scuec.demo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.List;

@RestController
@RequestMapping("/stu")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/pages")
    public PageInfo<StudentInfo> getPagesStudentInfo(
            @RequestParam(defaultValue = "1") Integer pageNum,  // 添加分页参数
            @RequestParam(defaultValue = "3") Integer pageSize) {

        System.out.println("接收到分页参数 - pageNum:" + pageNum + ", pageSize:" + pageSize); // 调试日志
        return studentService.getStudentInfoPages(pageNum, pageSize);
    }

    @GetMapping("/abc")
    public String getStudent() {
        return "student20250517";
    }



    @GetMapping("/list")
    public ResponseEntity<List<StudentInfo>> getStudentList() {
        return ResponseEntity.ok(studentService.getStudentInfo());
    }

    @GetMapping("/find")
    public ResponseEntity<StudentInfo> getStudentById(@RequestParam Integer id) {
        return ResponseEntity.ok(studentService.getStudentInfoById(id));
    }

    @DeleteMapping("/delete")
    public ResponseEntity<Integer> deleteStudentById(@RequestParam Integer id) {
        return ResponseEntity.ok(studentService.deleteStudentInfoById(id));
    }

    @PostMapping("/insert")
    public ResponseEntity<Integer> insertStudentInfo(@RequestBody StudentInfo studentInfo) {
        System.out.println("接收到的数据：" + studentInfo);
        return ResponseEntity.ok(studentService.insertStudentInfo(studentInfo));
    }

    // StudentController.java
    @PutMapping("/update")
    public ResponseEntity<Integer> updateStudentInfo(@RequestBody StudentInfo studentInfo) {
        System.out.println("更新数据：" + studentInfo);
        return ResponseEntity.ok(studentService.updateStudentInfo(studentInfo));
    }
}